function save_results_to_csv(n,best_params, filename)
% 保存结果到 CSV 文件
    results = cell(0);
    for i = 1 : length(best_params)
        if best_params(i).max_mf ~= -inf
            run = combineStructs(analyze_seq(best_params(i).best_sequences{1}),analyze_seq(best_params(i).best_sequences{2}));


            results{end+1} = [{n, best_params(i).max_mf,  best_params(i).best_peak, best_params(i).best_psl,best_params(i).best_isl, toc,best_params(i).str...
                dec2hex(bin2dec(num2str(best_params(i).best_sequences{1} ))), dec2hex(bin2dec(num2str(best_params(i).best_sequences{2} ))),...
                },struct2cell(run)'];

        end
    end

results = vertcat(results{:});
header = [{'N', 'MF', 'Peak', 'PSL', 'ISL' ,'runtime','Para', 'X', 'Y'},fieldnames(run)'];
results_table = cell2table(results, 'VariableNames', header);

% 如果文件存在，则追加数据；否则新建文件
if isfile(filename)
    writetable(results_table, filename, 'WriteVariableNames', false, 'WriteMode', 'append');
else
    writetable(results_table, filename);
end
end
function result = analyze_seq(x)
    n = length(x);
    run_seqs = [];
    current_len = 1;

    run1_max_len = 0;
    run1_count = 0;
    run0_max_len = 0;
    run0_count = 0;
    run1_len1_count = 0;

    for i = 2:n
        % 如果当前元素与前一个元素相同，增加当前长度
        if x(i) == x(i-1)
            current_len = current_len + 1;
        else
            % 将当前长度添加到序列
            run_seqs = [run_seqs current_len];
            if x(i-1)==1 && current_len==1
                run1_len1_count = run1_len1_count+1;
            end

            % 处理当前序列的计数和最大长度
            if x(i-1) == 1
                if current_len > run1_max_len
                    run1_max_len = current_len;
                    run1_count = 1;
                elseif current_len == run1_max_len
                    run1_count = run1_count + 1;
                end
            else
                if current_len > run0_max_len
                    run0_max_len = current_len;
                    run0_count = 1;
                elseif current_len == run0_max_len
                    run0_count = run0_count + 1;
                end
            end

            % 重置当前长度
            current_len = 1;
        end
    end

    % 处理最后一个序列
    run_seqs = [run_seqs current_len];

    if x(end) == 1
        if current_len > run1_max_len
            run1_max_len = current_len;
            run1_count = 1;
        elseif current_len == run1_max_len
            run1_count = run1_count + 1;
        end
    else
        if current_len > run0_max_len
            run0_max_len = current_len;
            run0_count = 1;
        elseif current_len == run0_max_len
            run0_count = run0_count + 1;
        end
    end

    % 过滤运行序列
    run_seqs = run_seqs(run_seqs > 0);

    % 计算差异
    run_count = length(run_seqs);
    
    run_count_diff = floor(n / 2) - run_count;
    run1_max_len_diff = run1_max_len - floor(sqrt(n));
    run0_max_len_diff = run0_max_len - floor(sqrt(n));

    % 创建结果结构
%    result.run_seqs = run_seqs;
    result.k = sum(x);

    result.k_diff = floor(n / 2) - result.k;

    result.run_count = run_count;
    result.run_count_diff = run_count_diff;
    result.run1_count = run1_count;
    result.run1_count_diff = floor(n / 4) - run1_count;
    result.run0_count = run0_count;
    result.run0_count_diff = floor(n / 4) - run0_count;
    result.run1_max_len = run1_max_len;
    result.run1_max_len_diff = run1_max_len_diff;
    result.run0_max_len = run0_max_len;
    result.run0_max_len_diff = run0_max_len_diff;
    result.run1_len1_count = run1_len1_count;
    result.run1_len1_count_diff = floor(result.k / 2) - run1_len1_count;
end
function combinedStruct = combineStructs(a, b)
% 检查两个结构体是否相同
if ~isequal(fieldnames(a), fieldnames(b))
    error('两个结构体的字段不匹配。');
end

% 初始化一个空结构体
combinedStruct = struct();

% 获取字段名
fields = fieldnames(a);

% 遍历每个字段，将对应的值组合成字符串
for i = 1:length(fields)
    fieldName = fields{i};
    % 将相应的值转换为字符串并组合
    combinedStruct.(fieldName) = sprintf('%s , %s',num2str(a.(fieldName)) , num2str(b.(fieldName)));
end
end
